package com.mc.developmentkit.views;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.mc.developmentkit.utils.PinyinComparator;
import com.mc.developmentkit.adapter.SortAdapter;
import com.mc.developmentkit.bean.SortModelInfo;
import com.mc.developmentkit.bean.LinkAgeInfo;
import com.mc.developmentkit.callback.LinkAgeListener;
import com.mc.developmentkit.utils.CharacterParser;
import com.qamaster.android.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * Created by LeBron on 2017/1/18.
 */

public class LinkAgeWindow extends PopupWindow {

    private ListView sortListView;
    private SideBar sideBar;
    private TextView dialog;
    private SortAdapter adapter;
    private ClearEditText mClearEditText;

    private CharacterParser characterParser;
    private List<SortModelInfo> SourceDateList;

    private PinyinComparator pinyinComparator;

    private List<LinkAgeInfo> linkAgeInfoList;

    private Context context;
    public PopupWindow popupWindow;

    public LinkAgeWindow(Context context) {
        this.context = context;
    }

    LinkAgeListener linkAgeListener;

    public void showPop(int weight,int height,View view,List<LinkAgeInfo> linkAgeInfoList, LinkAgeListener linkAgeListener,boolean showSideBar) {
        this.linkAgeListener = linkAgeListener;
        this.linkAgeInfoList=linkAgeInfoList;
        View contentView = LayoutInflater.from(context).inflate(
                R.layout.linkage_window, null);
        initViews(weight,height,contentView,view,showSideBar);
    }

    private void initViews(int w,int h,View view,View view1,boolean showSideBar) {
        characterParser = CharacterParser.getInstance();

        pinyinComparator = new PinyinComparator();

        sideBar = (SideBar) view.findViewById(R.id.sidrbar);
        if (showSideBar==true){
            sideBar.setVisibility(View.VISIBLE);
        }else{
            sideBar.setVisibility(View.GONE);
        }
        dialog = (TextView) view.findViewById(R.id.dialog);
        sideBar.setTextView(dialog);

        sideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {

            @Override
            public void onTouchingLetterChanged(String s) {

                int position = adapter.getPositionForSection(s.charAt(0));
                if (position != -1) {
                    sortListView.setSelection(position);
                }

            }
        });

        sortListView = (ListView) view.findViewById(R.id.country_lvcountry);
        sortListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                linkAgeListener.checkedResult(((SortModelInfo)adapter.getItem(position)).getName(), ((SortModelInfo)adapter.getItem(position)).getId());
                popupWindow.dismiss();
            }
        });

        ArrayList<String> list = new ArrayList<>();
        for (int j = 0; j < linkAgeInfoList.size(); j++) {
            String s = linkAgeInfoList.get(j).name;
            list.add(s);
        }

        SourceDateList = filledData(list);
        Collections.sort(SourceDateList, pinyinComparator);
        adapter = new SortAdapter(context, SourceDateList);
        sortListView.setAdapter(adapter);
        mClearEditText = (ClearEditText) view.findViewById(R.id.filter_edit);
        mClearEditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                filterData(s.toString());
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        popupWindow = new PopupWindow(view,
                w,h, true);
        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setBackgroundDrawable(context.getResources().getDrawable(
                R.color.gray_bg_shallow));
        // 设置好参数之后再show
        popupWindow.showAsDropDown(view1);

        // 使其聚集
        popupWindow.setFocusable(true);
        // 设置允许在外点击消失
        popupWindow.setOutsideTouchable(true);
        //刷新状态
        popupWindow.update();
    }


    private List<SortModelInfo> filledData(ArrayList<String> date) {
        List<SortModelInfo> mSortList = new ArrayList<SortModelInfo>();

        for (int i = 0; i < linkAgeInfoList.size(); i++) {
            SortModelInfo sortModelInfo = new SortModelInfo();
            sortModelInfo.setName(linkAgeInfoList.get(i).name);
            sortModelInfo.setId(linkAgeInfoList.get(i).id);
            String pinyin = characterParser.getSelling(linkAgeInfoList.get(i).name);
            String sortString = pinyin.substring(0, 1).toUpperCase();

            if (sortString.matches("[A-Z]")) {
                sortModelInfo.setSortLetters(sortString.toUpperCase());
            } else {
                sortModelInfo.setSortLetters("#");
            }

            mSortList.add(sortModelInfo);
        }
        return mSortList;

    }

    private void filterData(String filterStr) {
        List<SortModelInfo> filterDateList = new ArrayList<SortModelInfo>();

        if (TextUtils.isEmpty(filterStr)) {
            filterDateList = SourceDateList;
        } else {
            filterDateList.clear();
            for (SortModelInfo sortModelInfo : SourceDateList) {
                String name = sortModelInfo.getName();
                if (name.indexOf(filterStr.toString()) != -1 || characterParser.getSelling(name).startsWith(filterStr.toString())) {
                    filterDateList.add(sortModelInfo);
                }
            }
        }

        Collections.sort(filterDateList, pinyinComparator);
        adapter.updateListView(filterDateList);
    }

}
